﻿using PLCS.App.Shared.Common;
using PLCS.App.Shared.Models;
using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Client.Service.Client;
using PLCS.Shared.Common.Constant;

using RestSharp;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PLCS.App.Shared.Views.UserControls.Chlidren
{
    /// <summary>
    /// Stacker.xaml 的交互逻辑
    /// </summary>
    public partial class Stacker : UserControl
    {
        #region Properties

        //竖放标志
        private bool verticalflag;

        //左右上下侧边框
        public Rectangle leftline = new();

        public Rectangle rightline = new();
        public Rectangle topline = new();
        public Rectangle bottomline = new();

        //Webapi客户端
        public HttpRestClient restClient = new();

        private RequestParam requestParam;
        public SolidColorBrush bg;

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(Stacker), new UIPropertyMetadata(null));

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation,styling,binding,etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(Stacker), new UIPropertyMetadata(null));

        /// <summary>
        /// 强制输入事件和属性来自FrameworkElement
        /// </summary>
        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation,etc...
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(Stacker), new UIPropertyMetadata(null));

        /// <summary>
        /// 名称
        /// </summary>
        public string SName
        {
            get { return (string)GetValue(SNameProperty); }
            set { SetValue(SNameProperty, value); }
        }

        public static readonly DependencyProperty SNameProperty =
            DependencyProperty.Register("SName", typeof(string), typeof(Stacker), new PropertyMetadata("RGV01"));

        #endregion Properties

        public Stacker()
        {
            InitializeComponent();
            InitializeComponent();
            //小手
            Cursor = Cursors.Hand;
            SizeChanged += Stacker_SizeChanged;
        }

        private void Stacker_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ToolTip = $"Stacker:{SName}";
            rotate.Angle = gd.ActualWidth < gd.ActualHeight ? 90 : 0;
        }

        /// <summary>
        /// 重载鼠标左击时触发事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            //_eventAggregator.GetEvent<StationEvent>().Publish(SName);
            var command = Command;
            var parameter = CommandParameter;
            var target = CommandTarget;

            var routedCmd = command as RoutedCommand;
            if (routedCmd != null && routedCmd.CanExecute(parameter, target))
            {
                routedCmd.Execute(parameter, target);
            }
            else if (command != null && command.CanExecute(parameter))
            {
                command.Execute(parameter);
            }
        }

        #region Animation

        public void CraneMoveAnimation(string rowGoodsNo, string column)
        {
            var rowGoods = GlobalControl.Frame.FindName(rowGoodsNo) as RowGoods;
            if (rowGoods == null) return; if (rowGoods.Name == "0") return;
            double tovalue;
            double rowWidth = rowGoods.ActualWidth / rowGoods.ColumnCount;
            if (rowGoods.Platepos == "左")
                tovalue = rowWidth * (int.Parse(column) - 1) + Canvas.GetLeft(rowGoods) - (this.ActualWidth - rowWidth) / 2;
            else
                tovalue = rowWidth * (rowGoods.ColumnCount - int.Parse(column)) + Canvas.GetLeft(rowGoods) - (this.ActualWidth - rowWidth) / 2;
            double nowvalue = Canvas.GetLeft(this);
            Direction direction = Direction.X;

            //double time = Math.Abs(tovalue - nowvalue) / 100; // 时间 = 总长/速度
            double time = 0.1;
            AnimationHelper.FloatInElement(tovalue, direction, this, TimeSpan.FromSeconds(time));
        }

        #endregion Animation
    }
}